package com.kahriboo.log4mon.server;

import java.io.InputStream;
import java.net.Socket;

import com.kahriboo.log4mon.data.LogEventProtos;
import com.kahriboo.log4mon.data.LogEventProtos.LogEvent;

public class AppenderListener extends Thread {

	private final Socket socket;

	private AppenderListener(final Socket socket) {
		this.socket = socket;
	}

	public static void runListener(final Socket socket) {
		final AppenderListener runner = new AppenderListener(socket);
		runner.start();
	}

	@Override
	public void run() {
		try {
			System.out.println("Connection of a new Appender !!");

			final InputStream buf = socket.getInputStream();

			boolean isOpen = true;
			while (isOpen) {
				try {
					final LogEvent logEvent = LogEventProtos.LogEvent.parseDelimitedFrom(buf);
					// DataManager.getInstance().addData(logEvent);
				} catch (final Exception ex) {
					System.out.println("Client closed !!!!");
					isOpen = false;
				}
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}

	}

}
